﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyGiaoDuc_Winform.ServiceXuLyThongTinKhoa;
using QuanLyGiaoDuc_Winform.ServiceXuLyThongTinLop;
using QuanLyGiaoDuc_Winform.ServiceXuLyThongTinGiaoVien;

namespace QuanLyGiaoDuc_Winform
{
    public partial class FormPhanCongGiaoVien : Form
    {
        public FormPhanCongGiaoVien()
        {
            InitializeComponent();
        }

        public KHOA_getallResult[] list_khoa;
        public GIAOVIEN_getallwithMaKhoaResult[] list_gv;

        private void FormPhanCongGiaoVien_Load(object sender, EventArgs e)
        {
            ServiceXuLyThongTinKhoa.ServiceXuLyThongTinKhoaSoapClient proxy_Khoa = new ServiceXuLyThongTinKhoaSoapClient();

            list_khoa = proxy_Khoa.layDanhSachKhoa();

            foreach (KHOA_getallResult a in list_khoa)
            {
                cbb_khoa.Items.Add(a.MAKHOA + ". " + a.TENKHOA);
            }
        }
        
        ServiceXuLyThongTinLop.ServiceXuLyThongTinLopSoapClient proxy_lop = new ServiceXuLyThongTinLopSoapClient();
        ServiceXuLyThongTinGiaoVien.ServiceXuLyThongTinGiaoVienSoapClient proxy_gv = new ServiceXuLyThongTinGiaoVienSoapClient();
        

        private void cbb_khoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGrid_PhanCongGiaoVien.Rows.Clear();

            LOP_getallwithMaKhoaResult[] ds_lop = proxy_lop.layDanhSachLopTheoMaKhoa (list_khoa[cbb_khoa.SelectedIndex].MAKHOA);

            
            list_gv = proxy_gv.layDanhSachGiaoVienTheoMaKhoa(list_khoa[cbb_khoa.SelectedIndex].MAKHOA);

            
            for (int i = 0; i < ds_lop.Length; i++)
            {
                dataGrid_PhanCongGiaoVien.Rows.Add(i + 1, ds_lop[i].TENLOP, ds_lop[i].TENMONHOC, ds_lop[i].NGAYHOC, "tiết " + ds_lop[i].TIETBATDAU);
                //((DataGridViewComboBoxCell)dataGrid_PhanCongGiaoVien.Rows[i].Cells[5]).DataSource = list_gv;
                
                DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell ();
                cell.DataSource = list_gv;
                cell.DisplayMember = "TENGIAOVIEN";
                cell.ValueMember = "MAGIAOVIEN";


                dataGrid_PhanCongGiaoVien.Rows[i].Cells[5] = cell;
                dataGrid_PhanCongGiaoVien.Rows[i].Tag = ds_lop[i];

            }
        }
        
        
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGrid_PhanCongGiaoVien.Rows.Count; i++)
            {
                DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
                cell = (DataGridViewComboBoxCell)dataGrid_PhanCongGiaoVien.Rows[i].Cells[5];
                try
                {
                    if (cell.Value.ToString() != "")
                    {
                        LOP_getallwithMaKhoaResult lop_updated = new LOP_getallwithMaKhoaResult();
                        lop_updated = (LOP_getallwithMaKhoaResult)dataGrid_PhanCongGiaoVien.Rows[i].Tag;

                        proxy_lop.update_PhanCongGiaoVien(lop_updated.MALOP, int.Parse(cell.Value.ToString()));
                    }
                }
                catch
                {
                    continue;
                }

            }
            MessageBox.Show("Lưu dữ liệu thành công");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
